home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Applications 1996 May / SGI IRIX 6.2 Applications 1996 May.iso / dist / impr_dev.idb / usr / impressario / src / examples / libstiff / writeone.c.z / writeone.c
Encoding:
C/C++ Source or Header  |  1996-05-07  |  6.4 KB  |  217 lines

  1. /**************************************************************************
  2.  *
  3.  *           Copyright (c)    1992 Silicon Graphics, Inc.
  4.  *            All Rights Reserved
  5.  *
  6.  *       THIS    IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
  7.  *
  8.  * The copyright notice above does not evidence any actual of intended
  9.  * publication of such source code, and is an unpublished work by Silicon
  10.  * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
  11.  * the property of Silicon Graphics, Inc. Any use, duplication or
  12.  * disclosure not specifically authorized by Silicon Graphics is strictly
  13.  * prohibited.
  14.  *
  15.  * RESTRICTED RIGHTS LEGEND:
  16.  *
  17.  * Use, duplication or disclosure by the Government is subject to
  18.  * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
  19.  * Technical Data and Computer Software clause at DFARS 52.227-7013,
  20.  * and/or in similar or successor clauses in the FAR, DOD or NASA FAR
  21.  * Supplement. Unpublished - rights reserved under the Copyright Laws of
  22.  * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
  23.  * Shoreline Blvd., Mountain View, CA 94039-7311
  24.  **************************************************************************
  25.  *
  26.  * File: writeone.c
  27.  *
  28.  * Description: A libstiff example program that simply reads an X format
  29.  *    bitmap image and writes it to a STIFF format file. This program
  30.  *    demonstrates the use of the libstiff functions that write
  31.  *    STIFF files intended for printing. These are the so called Print
  32.  *    STIFF (PST) functions. These functions write a STIFF format file
  33.  *    with additional tags that may be of use to printer drivers and
  34.  *    filter programs.
  35.  *
  36.  *    The resulting STIFF image is written to stdout and can be sent to
  37.  *    a file using redirection.
  38.  *
  39.  * Usage: writeone
  40.  *
  41.  **************************************************************************/
  42.  
  43.  
  44. #ident "$Revision: 1.6 $"
  45.  
  46.  
  47. #include <stdio.h>
  48. #include "printstiff.h"
  49.  
  50.  
  51. /*
  52.  * Each byte of an X Bitmap files present the image data low order bit
  53.  * containing the data to be printed first and the high order bit containing
  54.  * the data to be printed last. A TIFF file needs the bits in the reverse
  55.  * order. Therefore, we will set up a union that will allow us to easily
  56.  * reverse the bits in each image byte.
  57.  */
  58.  
  59. typedef struct {
  60.     unsigned char b0:1;
  61.     unsigned char b1:1;
  62.     unsigned char b2:1;
  63.     unsigned char b3:1;
  64.     unsigned char b4:1;
  65.     unsigned char b5:1;
  66.     unsigned char b6:1;
  67.     unsigned char b7:1;
  68. } byte_t;
  69.  
  70. union {
  71.     unsigned char byte;
  72.     byte_t bits;
  73. } item;
  74.  
  75.  
  76. /*
  77.  * We will use the X bitmap that represents a 64 x 64 pixel X
  78.  * Logo. If a different bitmap is desired simply change the
  79.  * include and the size and data defines.
  80.  */
  81.  
  82. #include <X11/bitmaps/xlogo64>
  83.  
  84. #define IMAGE_WIDTH    xlogo64_width
  85. #define IMAGE_HEIGHT    xlogo64_height
  86. #define IMAGE_DATA    xlogo64_bits
  87.  
  88.  
  89. int main(int argc, char *argv[])
  90. {
  91.     STStream *sptr;
  92.     STImageHeader header;
  93.     PSTImageHeader pheader;
  94.     register int i, j;
  95.     register unsigned char c;
  96.     int amount;
  97.  
  98.     /*
  99.      * Compute amount of data to be written
  100.      */
  101.     amount = ((IMAGE_WIDTH + 7)/8 * 8 * IMAGE_HEIGHT) / 8;
  102.  
  103.     /*
  104.      * As indicated above a bit reversal must be performed
  105.      * on the bitmap image data. Perform that reversal
  106.      */
  107.     for (i = 0; i < amount; i++) {
  108.     item.byte = IMAGE_DATA[i];
  109.  
  110.     j = item.bits.b0;
  111.     item.bits.b0 = item.bits.b7;
  112.     item.bits.b7 = j;
  113.  
  114.     j = item.bits.b1;
  115.     item.bits.b1 = item.bits.b6;
  116.     item.bits.b6 = j;
  117.  
  118.     j = item.bits.b2;
  119.     item.bits.b2 = item.bits.b5;
  120.     item.bits.b5 = j;
  121.  
  122.     j = item.bits.b3;
  123.     item.bits.b3 = item.bits.b4;
  124.     item.bits.b4 = j;
  125.  
  126.     IMAGE_DATA[i] = item.byte;
  127.     }
  128.  
  129.     /*
  130.      * File in the Print STIFF Image header structure
  131.      */
  132.     /*        Base STIFF information */
  133.     pheader.width = IMAGE_WIDTH;
  134.     pheader.height = IMAGE_HEIGHT;
  135.     pheader.bitsPerSample = 1;
  136.     pheader.samplesPerPixel = 1;
  137.     pheader.imgbytes = amount;
  138.     pheader.type = ST_TYPE_K;
  139.     pheader.plane = ST_PLANE_PACKED;
  140.     /*        Print STIFF additional information */
  141.     pheader.resUnit = PST_RES_UNIT_INCH;
  142.     pheader.xRes = 300;
  143.     pheader.yRes = 300;
  144.     pheader.thresholding = PST_THRESHOLD_NONE;
  145.     pheader.compression = PST_COMPRESSION_NONE;
  146.     pheader.dateTime = "1992:10:02 23:10:12";
  147.     pheader.hostComputer = "SGI Iris";
  148.     pheader.software = argv[0];
  149.     pheader.docName = "X Bitmap";
  150.     pheader.pageNumbers[0] = 1;
  151.     pheader.pageNumbers[1] = 1;
  152.     pheader.targetPrinter = "Color Raster: HP DeskJet 500C";
  153.     pheader.driverOptions = "-n 1 -m 300";
  154.  
  155.     /*
  156.      * Display the header information that will be written
  157.      * to the file
  158.      */
  159.     fprintf(stderr, "PST Image Header\n");
  160.     /*        Base STIFF information */
  161.     fprintf(stderr, "\tImage width: %ld\n", pheader.width);
  162.     fprintf(stderr, "\tImage height: %ld\n", pheader.height);
  163.     fprintf(stderr, "\tBits per sample: %d\n", pheader.bitsPerSample);
  164.     fprintf(stderr, "\tSamples per pixel: %d\n", pheader.samplesPerPixel);
  165.     fprintf(stderr, "\tImage size (bytes): %ld\n", pheader.imgbytes);
  166.     fprintf(stderr, "\tImage type: %d\n", pheader.type);
  167.     fprintf(stderr, "\tData format: %d\n", pheader.plane);
  168.     /*        Print STIFF additional information */
  169.     fprintf(stderr, "\tResolution unit: %d\n", pheader.resUnit);
  170.     fprintf(stderr, "\tX Resolution: %d\n", pheader.xRes);
  171.     fprintf(stderr, "\tY Resolution: %d\n", pheader.yRes);
  172.     fprintf(stderr, "\tThresholding: %d\n", pheader.thresholding);
  173.     fprintf(stderr, "\tCompression: %d\n", pheader.compression);
  174.     fprintf(stderr, "\tDate/time: %s\n", pheader.dateTime);
  175.     fprintf(stderr, "\tHost computer: %s\n", pheader.hostComputer);
  176.     fprintf(stderr, "\tSoftware: %s\n", pheader.software);
  177.     fprintf(stderr, "\tDocument name: %s\n", pheader.docName);
  178.     fprintf(stderr, "\tCurrent page number: %d\n", pheader.pageNumbers[0]);
  179.     fprintf(stderr, "\tTotal pages: %d\n", pheader.pageNumbers[1]);
  180.     fprintf(stderr, "\tTarget printer: %s\n", pheader.targetPrinter);
  181.     fprintf(stderr, "\tDriver options: %s\n", pheader.driverOptions);
  182.  
  183.     /*
  184.      * Open a STIFF stream for the stdout
  185.      */
  186.     if ((sptr = STOpen(fileno(stdout), ST_WRITE)) == NULL) {
  187.     STPerror(argv[0]);
  188.     exit(1);
  189.     }
  190.  
  191.     /*
  192.      * Write a Print STIFF image header
  193.      */
  194.     if (PSTWriteImageHeader(sptr, &pheader, 0) < 0) {
  195.     STPerror(argv[0]);
  196.     exit(1);
  197.     }
  198.  
  199.     /*
  200.      * Wirte the image data
  201.      */
  202.     if (STWrite(sptr, IMAGE_DATA,  pheader.imgbytes) < 0) {
  203.     STPerror(argv[0]);
  204.     exit(1);
  205.     }
  206.  
  207.     /*
  208.      * Close the STIFF stream
  209.      */
  210.     if (STClose(sptr) < 0) {
  211.     STPerror(argv[0]);
  212.     exit(1);
  213.     }
  214.  
  215.     return 0;
  216. }
  217.